package cn;

import cn.webrx.mapper.BookMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;

import java.io.InputStream;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-09-19 14:28
 */
public class Demo {
    protected SqlSessionFactory sf;
    protected SqlSession ss;

    @Test
    public void tt(){
        BookMapper bookdao = ss.getMapper(BookMapper.class);
        System.out.println(bookdao.selectByPrimaryKey(34));
    }

    @Before
    public void init() {
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            sf = new SqlSessionFactoryBuilder().build(is);

            MapperHelper mh = new MapperHelper();
            mh.registerMapper(Mapper.class);
            mh.processConfiguration(sf.getConfiguration());

            ss = sf.openSession();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @After
    public void close() {
        ss.commit();
        ss.close();
    }
}
